package com.atguigu.utils

import java.util.Properties

import com.atguigu.utils.properties.PropertiesUtil
import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}

/**
 * redis连接池
 *
 * @author WangJX
 * @date 2019/11/26 14:55 
 * @version 1.0
 */
object RedisUtil {

  var jedisPool: JedisPool = null

  def getJedisPool(): Jedis ={
    if (jedisPool==null) {
      //获取配置文件信息
      val pro: Properties = PropertiesUtil.load("config.properties")
      val host: String = pro.getProperty("redis.host")
      val port: String = pro.getProperty("redis.port")

      val config = new JedisPoolConfig()
      config.setMaxTotal(100) //最大连接数
      config.setMaxIdle(20) //最大空闲
      config.setMinIdle(20) //最小空闲
      config.setBlockWhenExhausted(true)  //忙碌时是否等待
      config.setMaxWaitMillis(500)  //忙碌时等待时长 毫秒
      config.setTestOnBorrow(true)  //每次获得连接的进行测试

      jedisPool = new JedisPool(config, host, port.toInt)
    }
    jedisPool.getResource
  }

  def main(args: Array[String]): Unit = {

  }
}
